﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Hxsoft.AppV1.Module;
using Hxsoft.AppV1.WebUI;

namespace Hxsoft.AppV1._Controls
{
	[ValidationProperty("Value")]
	public partial class DicCascadingDropDownList : Hxsoft.AppV1.UserControl
	{
		public string Value
		{
			get { return CtrSelector.Value; }
			set { CtrSelector.Value = value; }
		}

		public bool OnlySelectLeaf
		{
			get { return CtrSelector.OnlySelectLeaf; }
			set { CtrSelector.OnlySelectLeaf = value; }
		}

		public int InitSelectorAmount
		{
			get { return CtrSelector.InitSelectorAmount; }
			set { CtrSelector.InitSelectorAmount = value; }
		}

		public string[] PromptTexts
		{
			get { return CtrSelector.PromptTexts; }
			set { CtrSelector.PromptTexts = value; }
		}

		private LoadedValue<int?> loadedSiteId;
		public int? SiteId
		{
			get
			{
				if (loadedSiteId.Loader == null)
					loadedSiteId.Loader = () => (int?)ViewState["SiteId"];
				return loadedSiteId.Value;
			}
			set
			{
				if (loadedSiteId.Setter == null)
					loadedSiteId.Setter = v =>
					{
						loadedClassId.ResetValue();
						ViewState["SiteId"] = v;
					};
				loadedSiteId.Value = value;
			}
		}

		private LoadedValue<string> loadedClassCode;
		public string ClassCode
		{
			get
			{
				if (loadedClassCode.Loader == null)
					loadedClassCode.Loader = () => (string)ViewState["ClassCode"];
				return loadedClassCode.Value;
			}
			set
			{
				if (loadedClassCode.Setter == null)
					loadedClassCode.Setter = v =>
					{
						loadedClassId.ResetValue();
						ViewState["ClassCode"] = v;
					};
				loadedClassCode.Value = value;
			}
		}

		private LoadedValue<int> loadedClassId;
		protected int ClassId
		{
			get
			{
				if (loadedClassId.Loader == null)
					loadedClassId.Loader = () => { return DicClasses.GetIdentifier(SiteId, ClassCode).Value; };
				return loadedClassId.Value;
			}
		}

		private LoadedValue<DicValueType> loadedValueType;
		public DicValueType ValueType
		{
			get
			{
				if (loadedValueType.Loader == null)
					loadedValueType.Loader = () => EnumExtensions.ConvertFromOrDefault<DicValueType>(ViewState["ValueType"], DicValueType.DicId);
				return loadedValueType.Value;
			}
			set
			{
				if (loadedValueType.Setter == null)
					loadedValueType.Setter = v => ViewState["ValueType"] = v;
				loadedValueType.Value = value;
			}
		}

		private LoadedValue<DicTextType> loadedTextType;
		public DicTextType TextType
		{
			get
			{
				if (loadedTextType.Loader == null)
					loadedTextType.Loader = () => EnumExtensions.ConvertFromOrDefault<DicTextType>(ViewState["TextType"], DicTextType.Name);
				return loadedTextType.Value;
			}
			set
			{
				if (loadedTextType.Setter == null)
					loadedTextType.Setter = v => ViewState["TextType"] = v;
				loadedTextType.Value = value;
			}
		}

		protected override void OnPreRender(EventArgs e)
		{
			CtrSelector.LoadDataUrl = GenericUrlManager.BuildDicCascadingDropDownListDataGeneratorUrl(SiteId, ClassCode, ValueType, TextType);
			CtrSelector.ValueContainerID = this.ClientID;

			if (!string.IsNullOrEmpty(Value))
			{
				List<string> values = new List<string>();

				Dictionary dic = ValueType == DicValueType.Value ? Dictionaries.GetItemByValue(ClassId, Value, true) : Dictionaries.GetItem(ClassId, int.Parse(Value), true);
				while (dic != null)
				{
					values.Insert(0, ValueType == DicValueType.Value ? dic.Value : dic.DicId.ToString());
					dic = dic.Parent;
				}

				CtrSelector.Values = values.ToArray();
			}

			base.OnPreRender(e);
		}
	}
}